﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RipPoll.Library;


namespace RipPoll.Web
{
    public partial class RegisterUser : RippollBasePage
    {
        int pollCategoryCount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Master != null)
                ((RippollMaster)this.Master).PageTitle = "Register User";

            if (!IsPostBack)
            {
                FillPollCategory();
                FillYearOfBirth();
            }
        }

        private void FillPollCategory()
        {
            PollServices.PollServicesClient psvc = new PollServices.PollServicesClient();
            List<PollCategoryPE> categories = psvc.GetPollcategories();
            if (categories != null)
            {
                for (int i = 0; i < categories.Count; i++ )
                {
                    PollCategoryPE pc = categories[i];
                    PollInterests.Items.Add(new ListItem(pc.Category, pc.Id.ToString()));
                }
                totalpollinterestcount.Value = categories.Count.ToString();
            }
        }

        private void FillYearOfBirth()
        {
            int curYear = DateTime.Today.Year;
            for (int i = curYear - 110; i < curYear - 5; i++)
            {
                ddlYearOfBirth.Items.Add(i.ToString());
            }
        }



        protected void btnCreateUser_Click(object sender, EventArgs e)
        {
            //Validate

            //check if pwd and confirm pwd are same
            string pwd = txtPassword.Text;
            string cpwd = txtConfirmPassword.Text;

            if (string.IsNullOrEmpty(txtFirstName.Text))
            {
                UpdateStatus("Firstname cannot be blank.");
                return;
            }

            if (string.IsNullOrEmpty(txtLastName.Text))
            {
                UpdateStatus("Firstname cannot be blank.");
                return;
            }

            if (string.IsNullOrEmpty(pwd))
            {
                UpdateStatus("Password cannot be blank.");
                return;
            }

            if (string.IsNullOrEmpty(pwd))
            {
                UpdateStatus("Confirm password cannot be blank.");
                return;
            }

            if (pwd != cpwd)
            {
                UpdateStatus("Password and confirm password doesn't match.");
                return;
            }

            if (string.IsNullOrEmpty(txtCity.Text))
            {
                UpdateStatus("City cannot be blank.");
                return;
            }

            if (string.IsNullOrEmpty(txtEmail.Text))
            {
                UpdateStatus("Email cannot be blank.");
                return;
            }


            UserServices.UserServicesClient ubo = new UserServices.UserServicesClient();
            UserPE prevUser = ubo.GetUserByEmailId(txtEmail.Text.Trim());
            if (prevUser != null)
            {
                UpdateStatus("User already registered with the same email id.");
                return;
            }

            UserPE newUser = new UserPE();
            newUser.FirstName = txtFirstName.Text;
            newUser.LastName = txtLastName.Text;
            newUser.PassWord = txtPassword.Text;
            newUser.BirthYear = int.Parse(ddlYearOfBirth.SelectedValue.ToString());

            newUser.City = txtCity.Text;
            newUser.Email = txtEmail.Text;
            //newUser.Gender = txt
            newUser.PollInterests = new List<UserPollInterestsPE>();

            foreach (ListItem li in PollInterests.Items)
            {
                if (li.Selected)
                {
                    UserPollInterestsPE upiPE = new UserPollInterestsPE() { PollCategoryId = int.Parse(li.Value) };
                    newUser.PollInterests.Add(upiPE);
                }
            }

            
            ubo.AddUser(newUser);

            UpdateStatus("Created user successfully.");
        }


        public override void Validate()
        {
            base.Validate();
        }
    }
}